<?php

/**
 * myprofile actions.
 *
 * @package    LYPO
 * @subpackage myprofile
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
 */
class myprofileActions extends sfActions
{

	public function preExecute(){
		if(sfConfig::get('sf_web_debug')==true){
			$this->setLayout('layout');
		}else{
			$this->setLayout(false);
		}
		$this->setTemplate('default');
	}

	/**
	 * Executes myprofile action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeProfile(sfWebRequest $request)
	{
		$result = new LypoResult();
		$userid = $request->getParameter('userid');
		if(!$userid){
			$result->setErrorMessage("No userid given!");
			return $this->renderText(print_r($result->toArray(),1));
		}
		try {
			/* @var $user User */
			$user = UserPeer::getByContainerString($userid);
			$culture = $this->getUser()->getCulture();
			if($user){
				$this->args = array('user' => $user, 'userid' => $userid, 'culture' => $culture);
				$this->partial = 'profile';
			}else{
				$result->setErrorMessage("User not found");
				return $this->renderText(print_r($result->toArray(),1));
			}
		}catch( Exception $e){
			$result->setErrorMessage($e->getMessage());
			return $this->renderText(print_r($result->toArray(),1));
		}
	}

	/**
	 * Executes myprofile action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeMystatements(sfWebRequest $request)
	{
		$result = new LypoResult();
		$userid = $request->getParameter('userid');
		$statement_page =   $this->getRequestParameter('statement_page',1);
		$debate_page =   $this->getRequestParameter('debate_page',1);
		$active_tab = $request->getParameter('active','statements');
		if(!$userid){
			$result->setErrorMessage("No userid given!");
			return $this->renderText(print_r($result->toArray(),1));
		}
		try {
			/* @var $user User */
			$user = UserPeer::getByContainerString($userid);
			if($user){
				$this->args = array('user' => $user, 'userid' => $userid, 'active_tab' => $active_tab);
				$myanswers = StatementAnswerPeer::getUserRegistrationAnswersPager($user,$statement_page,5);
				$this->args['myanswers'] = $myanswers;

				$mydebatereactions = LypoStatementPeer::getUserDebatesReactionsPager($user,$debate_page,5);
				$this->args['mydebatereactions'] = $mydebatereactions;
				if($mydebatereactions){
					$myanswer_ids = StatementAnswerPeer::getUserAnswersIds($user);
					$this->args['myanswer_ids'] = $myanswer_ids;
				}
				
				$reversed_opposites = $user->getReversedPerfectOpposites(5);
				$this->args['reversed_opposites'] = $reversed_opposites;
				
				$this->partial = 'mystatements';
			}else{
				$result->setErrorMessage("User not found");
				return $this->renderText(print_r($result->toArray(),1));
			}
		}catch( Exception $e){
			$result->setErrorMessage($e->getMessage());
			return $this->renderText(print_r($result->toArray(),1));
		}
	}

	public function executeOthers(sfWebRequest $request)
	{
		$result = new LypoResult();
		$userid = $request->getParameter('userid');
		$active_tab = $request->getParameter('active','perfectoppositeof');
		if(!$userid){
			$result->setErrorMessage("No userid given!");
			return $this->renderText(print_r($result->toArray(),1));
		}
		try {
			/* @var $user User */
			$user = UserPeer::getByContainerString($userid);
			if($user){
				$this->args = array('user' => $user, 'userid' => $userid, 'active_tab' => $active_tab, 'basehost' => $request->getHost());

				//Perfect opposites
				$reversed_opposites = $user->getReversedPerfectOpposites(5);
				$this->args['reversed_opposites'] = $reversed_opposites;
				
				//Respected by
				$data = UserPeer::getUsersWhoRespect($user);
				$this->args['respectedby_users'] = $data['users'];
				$this->args['respectedby_respects'] = $data['respects'];
				
				$this->partial = 'others';
			}else{
				$result->setErrorMessage("User not found");
				return $this->renderText(print_r($result->toArray(),1));
			}
		}catch( Exception $e){
			$result->setErrorMessage($e->getMessage());
			return $this->renderText(print_r($result->toArray(),1));
		}
	}
	
	

}
